package cc.rico.tutorial;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * ConnectionFactory 工具类
 * @author rico
 *
 */
@Slf4j
public class RabbitMQConnectionUtil {
	private static final String username = "dev";
	private static final String password = "123456";
	private static final String host = "192.168.223.150";
	private static final String vhost = "/develop";

	public static Connection getConnection() throws IOException, TimeoutException {
		log.info("RabbitMQConnectionUtil.getConnection...");

		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(host);
		factory.setUsername(username);
		factory.setPassword(password);
		factory.setVirtualHost(vhost);
		return factory.newConnection();
	}
	
	public static void close(Connection conn) throws IOException, TimeoutException {
		close(conn, null);
	}
	
	public static void close(Channel channel) throws IOException, TimeoutException {
		close(null, channel);
	}
	
	public static void close(Connection conn, Channel channel) throws IOException, TimeoutException {
		if(channel != null && channel.isOpen()) {
			channel.close();
		}
		
		if(conn != null) {
			conn.close();
		}

		log.info("RabbitMQConnectionUtil.close...");
	}
}
